package com.factory.test.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.base.BaseTestActivity;
import com.factory.test.R;
import com.factory.test.databinding.ActivityNetwork4gBinding;
import com.yecontech.sdk.base.util.SystemPropertiesProxy;

import java.io.DataInputStream;
import java.io.DataOutputStream;

/**
 * 网络4G测试
 */
public class Network4GActivity extends BaseTestActivity {



    private ActivityNetwork4gBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_network_4g);

        TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String imsi = mTelephonyMgr.getDeviceId();
        if (imsi == null) {
            binding.message.setText("4G imei is null");
            binding.message.setTextColor(Color.parseColor("#FF0000"));
            return;
        }

        int simStat = mTelephonyMgr.getSimState();
        if (simStat != 5){
            binding.message.setText("4G sim card not ready");
            binding.message.setTextColor(Color.parseColor("#FF0000"));
            return;
        }
        mTelephonyMgr.listen(new PhoneStateListener(){
            @Override
            public void onSignalStrengthsChanged(SignalStrength signalStrength) {
                super.onSignalStrengthsChanged(signalStrength);
                //这个dbm 是2G和3G信号的值, 正常手机信号变化范围是从 -110dBm (差)到 -50dBm (好)之间
                int asu = signalStrength.getGsmSignalStrength();
                int dbm = -113 + 2 * asu;


                String desc = "";
                if (dbm > -75) {
                    desc = "网络很好";
                    binding.signal.setTextColor(context.getColor(R.color.activity_btn_color_green));
                } else if (dbm > -85) {
                    desc = "网络不错";
                    binding.signal.setTextColor(context.getColor(R.color.green));
                } else if (dbm > -95) {
                    desc = "网络还行";
                    binding.signal.setTextColor(Color.YELLOW);
                } else if (dbm > -100) {
                    desc = "网络很差";
                    binding.signal.setTextColor(Color.RED);
                } else {
                    desc = "网络错误";
                    binding.signal.setTextColor(Color.RED);
                }
                binding.signal.setText(desc);
                binding.signal.append(", 信号强度: " + dbm + "dBm");
            }
        }, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

        String networktype = SystemInfoActivity.getNetworkToString(context);

        String baseband = SystemPropertiesProxy.getString(context, "gsm.version.baseband");
        binding.message.setText(
                "IMEI:" + imsi + "\n" +
                        "IMSI:" + mTelephonyMgr.getSubscriberId() + "\n" +
                        "ICCID:" + mTelephonyMgr.getSimSerialNumber() + "\n" +
                        "version:" + baseband + "\n" +
                        "sim:" + simStat + "\n" +
                        "netWorkType:" + networktype + "\n" +
                        "testing ping www.baidu.com ... \n");
        binding.message.setTextColor(getResources().getColor(R.color.activity_btn_color_green));

        new Thread(){
            @Override
            public void run() {
                ping();
            }
        }.start();
    }

    private void ping(){
        Process process = null;
        DataOutputStream dataOutputStream = null;

        DataInputStream dataInputStream = null;

        try {
            process = Runtime.getRuntime().exec("su");
            dataOutputStream = new DataOutputStream(process.getOutputStream());
            dataOutputStream.writeBytes( "ping -c 3 -w 5000 www.baidu.com \n");
            dataOutputStream.writeBytes("exit\n");
            dataOutputStream.flush();
            process.waitFor();

            dataInputStream = new DataInputStream(process.getInputStream());
            int available = dataInputStream.available();
            if(available > 0){
                byte[] by = new byte[available];
                dataInputStream.read(by);
                String result = new String(by);
                Log.d("TAG", "ping: "+result);
                Log.i("ping", result);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        binding.message.append(result);
                    }
                });
            }

            dataInputStream.close();
        } catch (Exception e) {
            //Log.e("adb", "shell exec fail ", e);
        } finally {
            try {
                if (dataOutputStream != null) {
                    dataOutputStream.close();
                }

                if (process != null) {
                    process.destroy();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
